﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Zj.BaiduPicSpider.Sys
{
    class HttpHelper
    {
        /// <summary>
        /// 模拟Http请求同步下载Html
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string DownloadHtml(string url,string referer)
        {
            HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
            req.KeepAlive = false;
            req.Timeout = 1000 * 30;
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            req.Host = url.Split('/')[2];
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
            req.Referer = referer;

            HttpWebResponse res = req.GetResponse() as HttpWebResponse;
            if (res.StatusCode != HttpStatusCode.OK)
            {
                return "";
            }
            using (var sr = new StreamReader(res.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }


        /// <summary>
        /// 模拟Http请求异步下载Html
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async static Task<string> DownloadHtmlAsync(string url)
        {
            HttpClient client = new HttpClient();
            return await client.GetAsync(url).Result.Content.ReadAsStringAsync();
        }


        /// <summary>
        /// 模拟Http请求同步下载图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <returns></returns>
        public static bool DownloadImg(string url, string referer,string path)
        {
            try
            {

                HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
                req.KeepAlive = false;
                req.Timeout = 1000 * 30;
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                req.Host = url.Split('/')[2];
                req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
                req.Referer = referer;

                HttpWebResponse res = req.GetResponse() as HttpWebResponse;
                if (res.StatusCode != HttpStatusCode.OK)
                {
                    return false;
                }
                using (var fs=new FileStream(path, FileMode.Create, FileAccess.Write))
                {
                    res.GetResponseStream().CopyTo(fs);
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}
